package com.cr.business.service.impl;

import com.cr.business.domain.vo.ChartCountVo;
import com.cr.business.mapper.TbQualifiedCertInfoMapper;
import com.cr.business.mapper.TbQualifiedCertPrintCountMapper;
import com.cr.business.mapper.TbUserEntityInfoMapper;
import com.cr.business.service.CountService;
import com.cr.common.core.domain.entity.SysDictData;
import com.cr.system.mapper.SysDictDataMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class CountServiceImpl implements CountService {

    @Resource
    private TbQualifiedCertInfoMapper tbQualifiedCertInfoMapper;

    @Resource
    private TbQualifiedCertPrintCountMapper tbQualifiedCertPrintCountMapper;

    @Resource
    private TbUserEntityInfoMapper tbUserEntityInfoMapper;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public Map<String, Object> getData() {

        // 合格证开具总数量
        int allCert = tbQualifiedCertInfoMapper.selectAllCount();
        // 合格证今日开具数量
        int todayCert = tbQualifiedCertInfoMapper.selectTodayCount();
        // 合格证打印总数量
        int allPrint = tbQualifiedCertPrintCountMapper.getSumPrintQuantity();
        // 合格证今日打印数量
        int todayPrint = tbQualifiedCertPrintCountMapper.getTodaySumPrintQuantity();

        // 主体数量
        int entityNum = tbUserEntityInfoMapper.selectAllCount();

        // 主体属性统计
        List<ChartCountVo> chartCountVos = tbUserEntityInfoMapper.selectAttrCount();
        List<SysDictData> dictData = sysDictDataMapper.selectDictDataByType("business_entity_attribute");
        List<String> entityAttrChartXAxis = new ArrayList<>();
        List<Integer> entityAttrChartData = new ArrayList<>();
        for (SysDictData sysDictData : dictData) {
            String dictValue = sysDictData.getDictValue();
            List<ChartCountVo> collect = chartCountVos.stream().filter(map -> dictValue.equals(map.getName())).collect(Collectors.toList());
            int num = 0;
            if (! collect.isEmpty()) {
                entityAttrChartData.add(collect.get(0).getValue());
            }
            entityAttrChartData.add(num);
            entityAttrChartXAxis.add(sysDictData.getDictLabel());
        }

        // 返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("allCert", allCert);
        result.put("todayCert", todayCert);
        result.put("allPrint", allPrint);
        result.put("todayPrint", todayPrint);
        result.put("entityNum", entityNum);
        result.put("entityAttrChartXAxis", entityAttrChartXAxis);
        result.put("entityAttrChartData", entityAttrChartData);
        return result;
    }
}
